package servlet.admin;

import com.google.gson.Gson;
import entity.Comment;
import service.CommentService;
import service.Impl.CommentServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/admin/CommentFindByName")
public class CommentFindByNameServlet extends HttpServlet {
    private CommentService commentService = new CommentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String userName = req.getParameter("userName");
        List<Comment> comments = commentService.findByUserName(userName);
        // 转换为 JSON 并返回
        Gson gson = new Gson();
        String json = gson.toJson(comments);
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
    }
}